﻿#include "mainform.h"
#include "ui_mainform.h"
#include "checkthread.h"
#include "iconhelper.h"
#include <QPainter>
#include <QDebug>
#include <QFileDialog>
#include <QClipboard>


#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif


MainForm::MainForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::MainForm)
{
    ui->setupUi(this);
    m_pCheckThread = NULL;
    m_nProgress = 0;
    m_eStep = CHECK_IDLE;

    setProperty("form", "MainForm");
    setAttribute(Qt::WA_StyledBackground);  // 禁止父窗口样式影响子控件样式
    qRegisterMetaType<ECheckStep>("ECheckStep");

    CreateAllChildWnd();
    InitCtrl();
    InitSolts();
    UpdateCtrlState();

    m_pCheckThread->start();
}

MainForm::~MainForm()
{
    m_pCheckThread->stop();
    m_pCheckThread->wait();

    delete ui;
}

void MainForm::CreateAllChildWnd()
{
#define NEW_OBJECT(pObj, TYPE) \
    if (NULL == pObj) { pObj = new TYPE(this); }

    NEW_OBJECT(m_pCheckThread, CheckThread);
}

void MainForm::InitCtrl()
{
    ui->pushButton->setProperty("choosebtn", true);
    ui->pushButton->setFixedHeight(75);
    ui->pushButton->setText(tr("Choose File"));
    ui->pushButton->setCursor(Qt::PointingHandCursor); //设置鼠标样式

    ui->widgetTop->setProperty("widgetTop", true);

    ui->label->setProperty("title", true);
    ui->label->setText(tr("文件检测助手"));

    IconHelper::SetIcon(ui->pushButton_copy, QChar(0xe657));
    IconHelper::SetIcon(ui->pushButton_Close, QChar(0xe609));
}

void MainForm::InitSolts()
{
    connect(m_pCheckThread, &QThread::finished, m_pCheckThread, &QObject::deleteLater);
    connect(m_pCheckThread, &CheckThread::SignalProgress, this, &MainForm::OnProgress);
}

void MainForm::UpdateCtrlState()
{
    ui->pushButton_copy->setVisible(CHECK_OVER == m_eStep);

    if (CHECK_OVER == m_eStep || CHECK_IDLE == m_eStep)
    {
        ui->pushButton->setCursor(Qt::PointingHandCursor); //设置鼠标样式
        ui->pushButton->setEnabled(true);
    }
    else
    {
        ui->pushButton->setCursor(Qt::ForbiddenCursor); //设置鼠标样式
        ui->pushButton->setEnabled(false);
    }
}

void MainForm::OnProgress(ECheckStep eStep, int nProgress)
{
    m_eStep = eStep;
    m_nProgress = nProgress;
    UpdateCtrlState();

    update();
}

void MainForm::paintEvent(QPaintEvent *)
{
    QPainter painter(this);         // 创建画家对象
    painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing, true); // 抗锯齿和使用平滑转换算法

    QFont ft = painter.font();

    QRect rcClient = rect();

    {
        painter.save();
        painter.setPen(QColor("#5D5D5F"));

        if (CHECK_IDLE == m_eStep)
        {
            QRect rcAvatar = QRect(130, 60, 300, 300);
            QPixmap p1 = QPixmap(":/res/bg.png").scaled(rcAvatar.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
            painter.drawPixmap(rcAvatar, p1);
        }
        else if (CHECK_OVER == m_eStep)
        {
            const int nStartH = 90;
            const int nSpaceH = 32;
            int nItem = 0;
            int nOffset = nItem * nSpaceH;

            TFileInfo tFileInfo = m_pCheckThread->GetFileInfo();
            qDebug() << tFileInfo.strFileName << tFileInfo.strFormat << tFileInfo.strMd5 << tFileInfo.strSha1;

            painter.save();

            painter.setPen(QColor("#0a0909"));
            painter.drawText(QRect(20, nStartH + nOffset, 120, 40), Qt::AlignLeft | Qt::AlignTop, tr("文件名:"));
            painter.setPen(QColor("#838383"));
            painter.drawText(QRect(150, nStartH + nOffset, 410, 40), Qt::AlignLeft | Qt::AlignTop, tFileInfo.strFileName);

            nOffset = (++nItem) * nSpaceH;
            painter.setPen(QColor("#0a0909"));
            painter.drawText(QRect(20, nStartH + nOffset, 120, 40), Qt::AlignLeft | Qt::AlignTop, tr("文件路径:"));
            painter.setPen(QColor("#838383"));
            painter.drawText(QRect(150, nStartH + nOffset, 410, 40), Qt::TextWordWrap | Qt::AlignLeft | Qt::AlignTop, tFileInfo.strFilePath);

            nOffset = (++nItem) * nSpaceH;
            painter.setPen(QColor("#0a0909"));
            painter.drawText(QRect(20, nStartH + nOffset, 120, 40), Qt::AlignLeft | Qt::AlignTop, tr("文件格式:"));
            painter.setPen(QColor("#838383"));
            painter.drawText(QRect(150, nStartH + nOffset, 410, 40), Qt::AlignLeft | Qt::AlignTop, tFileInfo.strFormat);

            nOffset = (++nItem) * nSpaceH;
            painter.setPen(QColor("#0a0909"));
            painter.drawText(QRect(20, nStartH + nOffset, 120, 40), Qt::AlignLeft | Qt::AlignTop, tr("文件大小:"));
            painter.setPen(QColor("#838383"));
            painter.drawText(QRect(150, nStartH + nOffset, 410, 40), Qt::AlignLeft | Qt::AlignTop, QString("%1 (%2 Bytes)").arg(tFileInfo.strSize).arg(tFileInfo.dwSize));

            nOffset = (++nItem) * nSpaceH;
            painter.setPen(QColor("#0a0909"));
            painter.drawText(QRect(20, nStartH + nOffset, 120, 40), Qt::AlignLeft | Qt::AlignTop, tr("创建时间:"));
            painter.setPen(QColor("#838383"));
            painter.drawText(QRect(150, nStartH + nOffset, 410, 40), Qt::AlignLeft | Qt::AlignTop, tFileInfo.strCreateTime);

            nOffset = (++nItem) * nSpaceH;
            painter.setPen(QColor("#0a0909"));
            painter.drawText(QRect(20, nStartH + nOffset, 120, 40), Qt::AlignLeft | Qt::AlignTop, tr("最后一次修改时间:"));
            painter.setPen(QColor("#838383"));
            painter.drawText(QRect(150, nStartH + nOffset, 410, 40), Qt::AlignLeft | Qt::AlignTop, tFileInfo.strModifyTime);

            nOffset = (++nItem) * nSpaceH;
            painter.setPen(QColor("#0a0909"));
            painter.drawText(QRect(20, nStartH + nOffset, 120, 40), Qt::AlignLeft | Qt::AlignTop, tr("最后一次读取时间:"));
            painter.setPen(QColor("#838383"));
            painter.drawText(QRect(150, nStartH + nOffset, 410, 40), Qt::AlignLeft | Qt::AlignTop, tFileInfo.strLastReadTime);

            nOffset = (++nItem) * nSpaceH;
            painter.setPen(QColor("#0a0909"));
            painter.drawText(QRect(20, nStartH + nOffset, 120, 40), Qt::AlignLeft | Qt::AlignTop, tr("MD5:"));
            painter.setPen(QColor("#838383"));
            painter.drawText(QRect(150, nStartH + nOffset, 410, 40), Qt::AlignLeft | Qt::AlignTop, tFileInfo.strMd5);

            nOffset = (++nItem) * nSpaceH;
            painter.setPen(QColor("#0a0909"));
            painter.drawText(QRect(20, nStartH + nOffset, 120, 40), Qt::AlignLeft | Qt::AlignTop, tr("SHA1:"));
            painter.setPen(QColor("#838383"));
            painter.drawText(QRect(150, nStartH + nOffset, 410, 40), Qt::AlignLeft | Qt::AlignTop, tFileInfo.strSha1);

            painter.restore();
        }
        else
        {
            QRect rcAvatar = QRect(180, 90, 200, 200);
            QPixmap p1 = QPixmap(":/res/load.png").scaled(rcAvatar.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
            painter.drawPixmap(rcAvatar, p1);

            switch (m_eStep)
            {
                case CHECK_MD5:
                {
                    painter.drawText(QRect(20, 317, 307, 20), Qt::AlignLeft | Qt::AlignTop, tr("正在检测MD5值..."));
                    break;
                }
                case CHECK_SHA1:
                {
                    painter.drawText(QRect(20, 317, 307, 20), Qt::AlignLeft | Qt::AlignTop, tr("正在检测SHA1值..."));
                    break;
                }
                default:
                {
                    painter.drawText(QRect(20, 317, 307, 20), Qt::AlignLeft | Qt::AlignTop, tr("正在检测文件..."));
                    break;
                }
            }

            QRect rcSlider1(20, 340, 485, 8);
            painter.setPen(Qt::NoPen);
            painter.setBrush(QColor("#E6EBF5"));
            painter.drawRoundedRect(rcSlider1, 5, 5);

            if (m_nProgress > 0)
            {
                QRect rcSel(20, 340, 485 * (double)m_nProgress/100.0, 8);
                painter.setPen(Qt::NoPen);
                painter.setBrush(QColor("#1890FF"));
                painter.drawRoundedRect(rcSel, 5, 5);
                //painter.drawRect(rcSel.marginsRemoved(QMargins(5, 0, 0, 0)));
            }

            painter.setPen(QColor("#5D5D5F"));
            painter.drawText(QRect(515, 335, 40, 40), Qt::AlignLeft | Qt::AlignTop, QString("%1%").arg(m_nProgress));

        }

        painter.restore();
    }
}

void MainForm::on_pushButton_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(this, tr(" 请选择一个文件"));

    if (fileName.length() > 0)
        m_pCheckThread->SetFilePath(fileName);
}

void MainForm::on_pushButton_Close_clicked()
{
    qApp->quit();
}

void MainForm::on_pushButton_copy_clicked()
{
    TFileInfo tFileInfo = m_pCheckThread->GetFileInfo();
    QString str = QString("%1 %2\r\n").arg(tr("文件名:")).arg(tFileInfo.strFileName);
    str.append(QString("%1 %2\r\n").arg(tr("文件路径:")).arg(tFileInfo.strFilePath));
    str.append(QString("%1 %2\r\n").arg(tr("文件格式:")).arg(tFileInfo.strFormat));
    str.append(QString("%1 %2 (%3 Bytes)\r\n").arg(tr("文件大小:")).arg(tFileInfo.strSize).arg(tFileInfo.dwSize));
    str.append(QString("%1 %2\r\n").arg(tr("创建时间:")).arg(tFileInfo.strCreateTime));
    str.append(QString("%1 %2\r\n").arg(tr("最后一次修改时间:")).arg(tFileInfo.strModifyTime));
    str.append(QString("%1 %2\r\n").arg(tr("最后一次读取时间:")).arg(tFileInfo.strLastReadTime));
    str.append(QString("%1 %2\r\n").arg(tr("MD5:")).arg(tFileInfo.strMd5));
    str.append(QString("%1 %2\r\n").arg(tr("SHA1:")).arg(tFileInfo.strSha1));

    QClipboard *board = QApplication::clipboard();
    board->setText(str);

}
